/****************************************************************************
** @brief concrete state subcalsses
****************************************************************************/

#include "stopingStatus.h"
#include "finishStatus.h"

#include "../oiovMain.h"
#include "../oiovCallback.h"
#include "../controllerMain.h"

#include "../../controller/controller_can.h"

#include <QDebug>
#define STOPINGSTATE_DEBUG

Status *StopingStatus::instance_ = nullptr;

StopingStatus::StopingStatus() : Status()
{
    state_ = Status::kStoping;
}

StopingStatus::~StopingStatus()
{

}

Status *StopingStatus::instance()
{
    if (nullptr == instance_) {
        instance_ = new StopingStatus();
    }
    return instance_;
}

void StopingStatus::Process(Gun *g)
{
    int stop_timeout;
    unsigned char no = g->gun_number();

    if ((CONGUNNOA != no) && (CONGUNNOB != no)) {
        return;
    }

    no = no - CONGUNNOA;

#if defined(STOPINGSTATE_DEBUG)
    static int count = 0;
    if (++count >= 5 * 1000 / 100) {
        count = 0;
        qDebug() << "[CHARGE]->" << (no ? "No.b" : "No.a") << "stoping state...";
    }
#endif

    switch (ControllerParserMain::con_telecommand[no].work_state) {
    case WAIT_STATE:
        ChangeState(g, FinishStatus::instance()); //切换到充电完成未拔枪

#if defined(STOPINGSTATE_DEBUG)
        qDebug() << "[CHARGE]->" << (no ? "No.b" : "No.a") << "stoping -> finish";
#endif

        //充电结束，不要上报充电中的属性数据了
        OiovReqMain::ClearChargingProperty(no);

        //设备状态 -> 充电结束未拔枪
        OiovCallback::ops_property_charge_data[no].status = OiovCallback::OPS_CHARGINGPILE_STATE_IDLE;
        //向平台发送设备状态事件
        OiovReqMain::SetDeviceStatusEvent(no);

        //上报交易记录
        if (OiovCallback::OPS_TRANSACTION_TYPE_APP == OiovCallback::ops_event_bill[no].transaction_type || OiovCallback::OPS_TRANSACTION_TYPE_ONCARD == OiovCallback::ops_event_bill[no].transaction_type) {
            OiovReqMain::SetStopChargeEvent(no);
        }
        break;
    case CONF_STATE:
        break;
    case CHARGING_STATE:
        if (g->stop_timeout() <= 0) {
            ChangeState(g, FinishStatus::instance()); //切换到充电完成未拔枪

#if defined(STOPINGSTATE_DEBUG)
            qDebug() << "[CHARGE]->" << (no ? "No.b" : "No.a") << "stoping -> finish";
#endif

            //充电结束，不要上报充电中的属性数据了
            OiovReqMain::ClearChargingProperty(no);

            //设备状态 -> 充电结束未拔枪
            OiovCallback::ops_property_charge_data[no].status = OiovCallback::OPS_CHARGINGPILE_STATE_IDLE;
            //向平台发送设备状态事件
            OiovReqMain::SetDeviceStatusEvent(no);

            //上报交易记录
            if (OiovCallback::OPS_TRANSACTION_TYPE_APP == OiovCallback::ops_event_bill[no].transaction_type || OiovCallback::OPS_TRANSACTION_TYPE_ONCARD == OiovCallback::ops_event_bill[no].transaction_type) {
                OiovReqMain::SetStopChargeEvent(no);
            }
        } else {
            stop_timeout = g->stop_timeout();
            g->set_stop_timeout(stop_timeout - 1);
        }
        break;
    case FULL_STATE:
        break;
    case WARRING_STATE:
        break;
    case FAULT_STATE:
        ChangeState(g, FinishStatus::instance()); //切换到充电完成未拔枪

#if defined(STOPINGSTATE_DEBUG)
        qDebug() << "[CHARGE]->" << (no ? "No.b" : "No.a") << "stoping -> finish";
#endif

        //充电结束，不要上报充电中的属性数据了
        OiovReqMain::ClearChargingProperty(no);

        //设备状态 -> 充电结束未拔枪
        OiovCallback::ops_property_charge_data[no].status = OiovCallback::OPS_CHARGINGPILE_STATE_IDLE;
        //向平台发送设备状态事件
        OiovReqMain::SetDeviceStatusEvent(no);

        //上报交易记录
        if (OiovCallback::OPS_TRANSACTION_TYPE_APP == OiovCallback::ops_event_bill[no].transaction_type || OiovCallback::OPS_TRANSACTION_TYPE_ONCARD == OiovCallback::ops_event_bill[no].transaction_type) {
            OiovReqMain::SetStopChargeEvent(no);
        }
        break;
    default:
        break;
    }
}
